package net.alche.fa.core;

import java.util.Map;
import java.util.List;
import net.alche.fa.core.model.Logic;
import net.alche.fa.core.model.FaParam;
import java.util.ArrayList;

public class FaMethod {
  public String methodName;
  public Map<String, String> def;
  public List<Logic> logicList;
  public Logic rootLogic;
  public List<FaParam> paramList;
  public String returnType;
  public Integer num;
  public String codeStr;

  private static FaParam parseParam(String typeStr, String varName) {
    FaParam param = new FaParam();
    if (typeStr.startsWith("var-")) {
      typeStr = typeStr.substring(4);
    }
    // param.type = FaValue.getChar(typeStr)
    // param.varName = varName
    // if param.type == FaValue._OBJECT
    // param.type = FaValue._SAGE
    // if typeStr.startsWith("List")
    // param.type = FaValue._LIST
    // else if typeStr.startsWith("Map")
    // param.type = FaValue._MAP
    //
    // param.objTypeName = typeStr
    //
    return param;
  }

  public static List<FaParam> getParamList(Logic logic, List<FaParam> params) {
    Logic next = null;
    Logic objLogic = null;
    if (params == null) {
      params = new ArrayList<>();
      next = logic.getLogicData().getLogicData();
      objLogic = next.getLogicObj();
      next = next.getLogicData();
    } else {
      objLogic = logic.getLogicObj();
      next = logic.getLogicData();
    }
    if (objLogic != null) {
      params.add(parseParam(objLogic.getWhat(), objLogic.getData()));
    }
    if (next != null) {
      return getParamList(next, params);
    }
    return params;
  }

  public String getDaoType() {
    String type = null;
    if (this.def != null) {
      type = this.def.get("Sql.type");
    }
    return type;
  }

  public String getAtTxt(String atCode) {
    if (this.getDef() == null) {
      return "";
    }
    String txt = this.getDef().get(atCode);
    if (txt == null) {
      txt = "";
    }
    return txt;
  }

  public Integer getNum() {
    return num;
  }

  public String getCodeStr() {
    return codeStr;
  }

  public String getMethodName() {
    return methodName;
  }

  public void setMethodName(String methodName) {
    this.methodName = methodName;
  }

  public Map<String, String> getDef() {
    return def;
  }

  public void setDef(Map<String, String> def) {
    this.def = def;
  }

  public List<Logic> getLogicList() {
    return logicList;
  }

  public void setLogicList(List<Logic> logicList) {
    this.logicList = logicList;
  }

  public List<FaParam> getParamList() {
    return paramList;
  }

  public void setParamList(List<FaParam> paramList) {
    this.paramList = paramList;
  }

  public Logic getRootLogic() {
    return rootLogic;
  }

  public void setRootLogic(Logic rootLogic) {
    this.rootLogic = rootLogic;
  }

  public String getReturnType() {
    return returnType;
  }

  public void setReturnType(String returnType) {
    this.returnType = returnType;
  }
}